home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / dte5_1.zip / HWSYSV.C < prev    next >
C/C++ Source or Header  |  1991-02-06  |  44KB  |  1,516 lines

  1. /*
  2.  * Written by Douglas Thomson (1989/1990)
  3.  * Modified for pure System V release 2 by Jay Maynard (1990)
  4.  *
  5.  * This source code is released into the public domain.
  6.  */
  7.  
  8. /*
  9.  * Name:    dte - Doug's Text Editor program - hardware dependent module
  10.  * Purpose: This file contains all the code that needs to be different on
  11.  *           different hardware.
  12.  * File:    hwsysv.c
  13.  * Authors: Douglas Thomson and Jay Maynard
  14.  * System:  This particular version is for generic System V release 2,
  15.  *           although it should suit most versions of UNIX system V.
  16.  * Date:    28 November 1990
  17.  * Notes:   This module has been kept as small as possible, to facilitate
  18.  *           porting between different systems.
  19.  */
  20. #include <curses.h>     /* used to access terminfo database */
  21. #include <term.h>       /* used to set terminal modes */
  22. #include <fcntl.h>      /* used to set up stdin with no delay reads */
  23. #include <signal.h>     /* used to trap various signals (future?) */
  24. #include <varargs.h>    /* used to pass a variable no. of arguments */
  25. #include "common.h"     /* dte types */
  26. #include "hwdep.h"      /* prototypes for functions here */
  27. #include "utils.h"      /* prototypes for display/input etc */
  28. #include "version.h"    /* current version number */
  29.  
  30. /*
  31.  * A bug in some versions of elm causes editing sessions not to be killed
  32.  *  when elm is killed (for example when a modem user unplugs the 'phone
  33.  *  while editing mail!).
  34.  * The editor process is inherited by the init process, so all I do here
  35.  *  is have the editor commit suicide if it detects that its parent has
  36.  *  died.
  37.  */
  38. #define ELM_BUG
  39.  
  40. /*
  41.  * prototypes for all functions in this file
  42.  */
  43. void error ARGS((int kind, ...));
  44. static void myputchar ARGS((char c));
  45. static void hw_attr ARGS((char attr));
  46. static void att_check ARGS((void));
  47. void att_stuff ARGS((void));
  48. void hw_xygoto ARGS((void));
  49. int hw_clreol ARGS((void));
  50. int hw_linedel ARGS((int line));
  51. int hw_lineins ARGS((int line));
  52. int hw_backspace ARGS((void));
  53. int hw_c_insert ARGS((void));
  54. int hw_c_delete ARGS((void));
  55. void hw_c_output ARGS((int c));
  56. void hw_terminate ARGS((void));
  57. static void process_input ARGS((void));
  58. void hw_initialize ARGS((void));
  59. int hw_c_avail ARGS((void));
  60. int hw_c_input ARGS((void));
  61. void main ARGS((int argc, char *argv[]));
  62. void hw_move ARGS((text_ptr dest, text_ptr source, long number));
  63. int hw_rename ARGS((char *old, char *new));
  64. int hw_scroll_up ARGS((int top, int bottom));
  65. int hw_scroll_down ARGS((int top, int bottom));
  66. int min ARGS((int a, int b));
  67. int hw_fattrib ARGS((char *name));
  68. int hw_set_fattrib ARGS((char *name, int attrib));
  69. int hw_unlink ARGS((char *name));
  70. int hw_printable ARGS((int c));
  71. int hw_load ARGS((char *name, text_ptr start, text_ptr limit, text_ptr *end));
  72. static int write_file ARGS((char *name, char *mode, text_ptr start,
  73.         text_ptr end));
  74. int hw_save ARGS((char *name, text_ptr start, text_ptr end));
  75. int hw_append ARGS((char *name, text_ptr start, text_ptr end));
  76. int hw_print ARGS((text_ptr start, text_ptr end));
  77. void hw_copy_path ARGS((char *old, char *name, char *new));
  78.  
  79. #define REVERSE 1       /* reverse video (or standout) attribute */
  80. #define HIGH 2          /* high intensity (or underline) attribute */
  81. #define NORMAL 3        /* normal video attribute */
  82.  
  83. #define UNKNOWN 7       /* flag not yet set to TRUE or FALSE */
  84.  
  85. char *malloc(/*!void*/);     /* memory allocator - this keeps lint happy */
  86.  
  87. /*
  88.  * the following variable determines the size of the memory buffer used. It
  89.  *  is set to something reasonable in main.
  90.  */
  91. static int g_space = 0;
  92.  
  93. /*
  94.  * Name:    error
  95.  * Purpose: To report an error, and usually make the user type <ESC> before
  96.  *           continuing.
  97.  * Date:    October 10, 1989
  98.  * Passed:  kind:   an indication of how serious the error was:
  99.  *                      TEMP:    merely a message, do not wait for <ESC>
  100.  *                      DIAG:    merely a message, but make sure user sees it
  101.  *                      WARNING: error, but editor can continue after <ESC>
  102.  *                      FATAL:   abort the editor!
  103.  *          format: printf format string for any arguments that follow
  104.  *          ...:    arguments to be printed
  105.  * Notes:   This function should be system independent; that is the whole
  106.  *           point of the "varargs" philosophy. However, two of the systems
  107.  *           I have used implemented "varargs" incompatibly, and some older
  108.  *           systems may not support the "varargs" macros at all...
  109.  */
  110. void error(kind, va_alist)
  111. int kind;
  112. va_dcl
  113. {
  114.     char *format;           /* printf format string for error message */
  115.     va_list argptr;         /* used to access various arguments */
  116.     char buff[MAX_COLS];    /* somewhere to store error before printing */
  117.     int c;                  /* character entered by user to continue */
  118.  
  119.     /*
  120.      * obtain the first two arguments
  121.      */
  122.     va_start(argptr);
  123.     format = va_arg(argptr, char *);
  124.  
  125.     /*
  126.      * tell the user what kind of an error it is
  127.      */
  128.     switch (kind) {
  129.     case FATAL:
  130.         strcpy(buff, "Fatal error: ");
  131.         break;
  132.     case WARNING:
  133.         strcpy(buff, "Warning: ");
  134.         break;
  135.     case DIAG:
  136.     case TEMP:
  137.         strcpy(buff, "");
  138.         break;
  139.     }
  140.  
  141.     /*
  142.      * prepare the error message itself
  143.      */
  144.     vsprintf(buff + strlen(buff), format, argptr);
  145.     va_end(argptr);
  146.  
  147.     /*
  148.      * tell the user how to continue editing if necessary
  149.      */
  150.     if (kind == WARNING || kind == DIAG) {
  151.         strcat(buff, ": type <ESC>");
  152.     }
  153.  
  154.     /*
  155.      * output the error message
  156.      */
  157.     set_prompt(buff, 1);
  158.  
  159.     if (kind == FATAL) {
  160.         /*
  161.          * no point in making the user type <ESC>, since the program is
  162.          *  about to abort anyway...
  163.          */
  164.         terminate();
  165.         exit(1);
  166.     }
  167.     else if (kind != TEMP) {
  168.         /*
  169.          * If necessary, force the user to acknowledge the error by
  170.          *  typing <ESC> (or ^U).
  171.          * This prevents any extra commands the user has entered from
  172.          *  causing problems after an error may have made them inappropriate.
  173.          */
  174.         while ((c=c_input()) != 27 && c != CONTROL('U')) {
  175.             set_prompt(buff, 1);
  176.         }
  177.     }
  178. }
  179.  
  180. /*
  181.  * Name:    myputchar
  182.  * Purpose: To output one character to the display terminal.
  183.  * Date:    October 10, 1989
  184.  * Passed:  c: character to be displayed
  185.  * Notes:   This function makes the write system call directly, to try to
  186.  *           minimize the amount of buffering that takes place. (Since this
  187.  *           editor tries to respond quickly to new commands, we do not
  188.  *           want a whole screenful of output stored in a buffer waiting
  189.  *           to be sent to the terminal!)
  190.  *          For very high speed terminals, it may be more appropriate to
  191.  *           encourage buffering... writing just one character has the
  192.  *           disadvantage that under heavy load screen update sometimes
  193.  *           "freezes" temporarily while other processes run!
  194.  */
  195. static void myputchar(c)
  196. char c;
  197. {
  198.     putc(c, stdout);
  199. /*    write(1, &c, 1); */
  200. }
  201.  
  202. /*
  203.  * Name:    hw_attr
  204.  * Purpose: To select a new attribute on the terminal.
  205.  * Date:    October 10, 1989
  206.  * Passed:  attr: the desired attribute
  207.  */
  208. static void hw_attr(attr)
  209. char attr;
  210. {
  211.     static int old_att = -1;         /* existing attribute */
  212.  
  213.     /*
  214.      * If there has been no change, then ignore the call (actually this
  215.      *  should never happen, since hw_attr is only called when the
  216.      *  attribute HAS changed...
  217.      */
  218.     if (old_att == attr) {
  219.         return;
  220.     }
  221.  
  222.     /*
  223.      * If we want the normal attribute, then there may be an easy way of
  224.      *  getting it without undoing other attributes.
  225.      */
  226.     if (attr == g_display.normal) {
  227.         if (exit_attribute_mode) {
  228.             tputs(exit_attribute_mode, 1, myputchar);
  229.             old_att = attr;
  230.             return;
  231.         }
  232.     }
  233.  
  234.     /*
  235.      * end the current attribute
  236.      */
  237.     if (old_att != g_disp